#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

//0x80 + 0x00 + 0x01 = 0x81=129  客厅空调一档
//0x80 + 0x20 + 0x01 = 0xA1 = 161 客厅灯打开

#define DEV_UART 			"/dev/ttyUSB0"

void serial_init(int fd)
{
    struct termios options;
    tcgetattr(fd, &options);
    options.c_cflag |= ( CLOCAL | CREAD );
    options.c_cflag &= ~CSIZE;
    options.c_cflag &= ~CRTSCTS;
    options.c_cflag |= CS8;
    options.c_cflag &= ~CSTOPB;
    options.c_iflag |= IGNPAR;
    options.c_iflag &= ~(ICRNL | IXON);
    options.c_oflag = 0;
    options.c_lflag = 0;

    cfsetispeed(&options, B115200);
    cfsetospeed(&options, B115200);
    tcsetattr(fd,TCSANOW,&options);
}


int main(int argc, char *argv[])
{
	if(argc < 2){
		printf("arg err");
		exit(-1);
	}
	unsigned char dev_uart_mask = atoi(argv[1]);
	int dev_uart_fd;
	
    if ((dev_uart_fd = open (DEV_UART, O_RDWR)) < 0)
    {
        perror ("open uart err");
        return -1;
    }
	
	serial_init (dev_uart_fd);
	
	
	write(dev_uart_fd, &dev_uart_mask, 1);
	
	sleep(1);
}

